/*
 * @Author: gyc
 * @Date: 2020-05-26 13:18:40
 * @LastEditTime: 2020-08-26 16:36:37
 * @Description: Nuxt测试版
 */
'use strict';

const Controller = require('egg').Controller;
const AES = require("../../config/utils")
class UserController extends Controller {

	async index() {
		console.log(this.ctx.isAuthenticated())
		this.ctx.body = {
			msg: 'hello!',
			user: this.ctx.user
		};
	}
	//注册用户
	async create() {
		const {
			ctx
		} = this;
		const username = ctx.request.body.username
		const password = ctx.request.body.password
		let user = await ctx.service.user.createUser(username, password)
		ctx.body = {
			data: user
		};
	}
	//登录状态查询
	async isAuthenticated() {
		const {
			ctx
		} = this;
		//未登录情况
		if (ctx.isAuthenticated()) {
			const user = await ctx.service.user.findOneInfo(ctx.user.username)
			ctx.body = {
				data: ctx.isAuthenticated(),
				isAdmin: user.administrator,
				userid: user.id
			};
		} else {
			ctx.body = {
				data: ctx.isAuthenticated(),
			};
		}
	}
	//退出登录
	async logout() {
		const {
			ctx
		} = this;
		ctx.logout()
		ctx.body = {
			data: ctx.user
		};
	}
	//登录
	async login() {
		const {
			ctx
		} = this;
		if (ctx.isAuthenticated()) {
			this.ctx.login({
				username: ctx.user.username,
				password: ctx.user.password
			})
			const userid = await ctx.service.user.findOneId(ctx.user.username)
			ctx.body = {
				code: "200",
				msg: "success Login",
				data: ctx.user,
				ip: ctx.request,
				userid: userid
			};
		} else {
			ctx.body = {
				code: "201",
				msg: "no auth",
				data: ctx.user
			};
		}
	}
	//登录失败
	async badLogin() {
		const {
			ctx
		} = this;
		ctx.body = {
			code: "500",
			msg: "Bad Login",
			data: ctx.user
		};
	}
	//用户信息查询
	async getUserinfo() {
		const {
			ctx
		} = this;
		const userid = ctx.query.userid
		const user = await ctx.service.user.findUserinfo(userid)
		ctx.body = {
			code: "200",
			data: user
		};
	}
	//用户信息更新
	async updateUserinfo() {
		const {
			ctx
		} = this;
		const userid = ctx.query.userid
		const data = {
			nickName: ctx.query.nickName,
			sex: ctx.query.sex,
			hobby: ctx.query.hobby,
			bestStory: ctx.query.bestStory,
			QQ: ctx.query.QQ,
			signature: ctx.query.signature
		}
		const user = await ctx.service.user.updateUserinfo(userid, data)
		ctx.body = {
			code: "200",
			data: user
		};
	}
	//用户数
	async userCount() {
		const {
			ctx
		} = this;
		const count = await ctx.service.user.userCount()
		ctx.body = {
			code: "200",
			data: count
		}
	}
	//用户密码修改
	async userReword() {
		const {
			ctx
		} = this;
		const userid = ctx.query.userid
		const newPwd = ctx.query.newPwd
		const oldPwd = ctx.query.oldPwd
		console.log(userid, newPwd, oldPwd)
		if (userid && newPwd && oldPwd) {
			const result = await ctx.service.user.userReword(userid, oldPwd, newPwd)
			ctx.body = {
				code: result.code,
				data: result.data
			}
		} else {
			ctx.body = {
				code: "500",
				data: '参数不全!'
			}
		}

	}
	//用户状态管理
	async userStusCtl() {
		const {
			ctx
		} = this;
		const userid = ctx.query.userid
		const stus = ctx.query.stus
		if (stus === '1' || stus === '0') {
			const result = await ctx.service.user.userStusCtl(userid, stus)
			ctx.body = {
				code: '200', data: result
			}
		}else{
			ctx.body = {
				code: '500', data: 'stus参数异常！'
			}
		}

	}
}

module.exports = UserController;